﻿using A.M.ETO;
using A.M.Storage.Application;
using A.M.Storage.Infrastruecure;
using L.M.System.Application.IServices;
using L.M.System.Domain.DTO;
using L.M.System.Domain.Entity;
using L.M.System.Infrastruecure.IRepository;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace L.M.System.Application.Services
{
    public class Working_hoursServices : BaseServices<working_hours, string>, IWorking_hoursServices
    {
        private readonly IWorking_hoursRepository working_HoursRepository;
        public Working_hoursServices(IWorking_hoursRepository working_HoursRepository) : base(working_HoursRepository)
        {
            this.working_HoursRepository = working_HoursRepository;
        }

        /// <summary>
        /// 修改工作时间
        /// </summary>
        /// <param name="works">工作日</param>
        /// <returns>修改工作时间</returns>
        public async Task<ApiReturn<working_hours>> AddWorkInfor(WorkInforDTO works)
        {
            ApiReturn<working_hours> apiReturn = new ApiReturn<working_hours>();
            try
            {
                //统一传输格式
                if (works.work_morning_run == "" || works.work_morning_run == null)
                {
                    works.work_morning_run = "";
                }
                if (works.work_morning_fin == "" || works.work_morning_fin == null)
                {
                    works.work_morning_fin = "";
                }
                if (works.work_afternoon_run == "" || works.work_afternoon_run == null)
                {
                    works.work_afternoon_run = "";
                }
                if (works.work_afternoon_fin == "" || works.work_afternoon_fin == null)
                {
                    works.work_afternoon_fin = "";
                }
                if (works.work_weekid != "")
                {

                    var whid = 1;
                    List<working_hours> hours = new List<working_hours>();
                    double morninginfor = 0;
                    if (works.work_morning_run != "")
                    {
                        //上午起始
                        var Kmorning = works.work_morning_run.Split(":");
                        var Kmoneyhoust = Convert.ToInt32(Kmorning[0]) * 60 + Convert.ToInt32(Kmorning[1]);
                        //上午终止
                        if(works.work_morning_fin == "")
                        {
                            apiReturn.Code = 500;
                            apiReturn.Message = "请选择上午结束时间";
                            return apiReturn;
                        }
                        var Jmorning = works.work_morning_fin.Split(":");
                        var Jmorninghoust = Convert.ToInt32(Jmorning[0]) * 60 + Convert.ToInt32(Jmorning[1]);
                        //早上工作时长
                         morninginfor = (Convert.ToDouble(Jmorninghoust) - Convert.ToDouble(Kmoneyhoust)) / 60;

                    }
                    double afternooninfor = 0;
                    if (works.work_afternoon_run != "" )
                    {
                        //下午工作时间
                        //下午起始
                        var Kafternoon = works.work_afternoon_run.Split(":");
                        var Kafternoonhoust = Convert.ToInt32(Kafternoon[0]) * 60 + Convert.ToInt32(Kafternoon[1]);
                        //下午终止
                        if (works.work_afternoon_fin == "")
                        {
                            apiReturn.Code = 500;
                            apiReturn.Message = "请选择下午结束时间";
                            return apiReturn;
                        }
                        var Jafternoon = works.work_afternoon_fin.Split(":");
                        var Jafternoonhoust = Convert.ToInt32(Jafternoon[0]) * 60 + Convert.ToInt32(Jafternoon[1]);
                        //下午工作时长
                         afternooninfor = (Convert.ToDouble(Jafternoonhoust) - Convert.ToDouble(Kafternoonhoust)) / 60;
                    }
                   

                    var array = works.work_weekid.Split(",");
                    foreach (var item in array)
                    {
                        working_hours working_Hours = new working_hours();
                        working_Hours.whid =Convert.ToInt32(item);
                        switch (item)
                        {
                            case "1": working_Hours.work_week = "星期一";  break;
                            case "2": working_Hours.work_week = "星期二"; break;
                            case "3": working_Hours.work_week = "星期三"; break;
                            case "4": working_Hours.work_week = "星期四"; break;
                            case "5": working_Hours.work_week = "星期五"; break;
                            case "6": working_Hours.work_week = "星期六"; break;
                            case "7": working_Hours.work_week = "星期日"; break;
                        }
                        working_Hours.work_summioeny = morninginfor.ToString("0.00");
                        working_Hours.work_sumevent = afternooninfor.ToString("0.00");
                        if (works.work_morning_run != "")
                        {
                            working_Hours.work_morning = works.work_morning_run + " - " + works.work_morning_fin;
                        }
                        else
                        {
                            working_Hours.work_morning = "放假";
                        }
                        
                        if (works.work_afternoon_run != "")
                        {
                            working_Hours.work_afternoon = works.work_afternoon_run + " - " + works.work_afternoon_fin;
                        }
                        else
                        {
                            working_Hours.work_afternoon = "放假";
                        }
                        hours.Add(working_Hours);
                    }
                    int res1 = await working_HoursRepository.UpdInfor_Batch_Async(hours);
                    if (res1 > 0)
                    {
                        apiReturn.Code = 200;
                        apiReturn.Message = "修改成功";
                    }
                    else
                    {
                        apiReturn.Code = 500;
                        apiReturn.Message = "修改失败";
                    }
                }
            }
            catch (Exception)
            {


            }
            return apiReturn;
        }
    }
}
